package nickthissen.iracing.adapters;

import java.util.ArrayList;

import nickthissen.iracing.activities.ItemListActivity;
import nickthissen.iracing.common.DateUtils;
import nickthissen.iracing.common.Prefs;
import nickthissen.iracing.datamodels.PrivateMessageItem;
import nickthissen.iracing.iracingforum2.R;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PmListAdapter extends ArrayAdapter<PrivateMessageItem>
{
	private ItemListActivity<PrivateMessageItem> activity;
	private ArrayList<PrivateMessageItem> items;

	private boolean showAuthor, showDate;
	
	public PmListAdapter(ItemListActivity<PrivateMessageItem> context, int resourceId, ArrayList<PrivateMessageItem> items)
	{
		super(context, resourceId, items);
		this.activity = context;
		this.items = items;

        showAuthor = Prefs.get(context).getBoolean(R.string.PREF_PmListShowAuthor, true);
        showDate = Prefs.get(context).getBoolean(R.string.PREF_PmListShowDate, true);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		if (v == null)
		{
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.pmrow, null);
		}
		
		PrivateMessageItem pm = items.get(position);
		if (pm != null)
		{			
			v.setClickable(true);
			v.setOnClickListener(new ItemClickListener(pm));
			v.setOnCreateContextMenuListener(null);
			
			TextView lblTitle = (TextView) v.findViewById(R.id.lblTop);
			TextView lblBottom = (TextView) v.findViewById(R.id.lblBottom);
			LinearLayout background = (LinearLayout) v.findViewById(R.id.background);
			ImageView imgIcon = (ImageView) v.findViewById(R.id.imgIcon);
				
			lblTitle.setMaxLines(1);
			lblBottom.setMaxLines(1);
			
			lblTitle.setText(pm.Title);

			ArrayList<String> subtexts = new ArrayList<String>();
			if (showAuthor) subtexts.add(pm.Author);
			if (showDate) subtexts.add(DateUtils.UnixMsToString(pm.CreatedTime));
			
			lblBottom.setText(StringUtils.join(subtexts, ",  "));
			if (subtexts.isEmpty()) lblBottom.setVisibility(View.GONE);
								
			background.setBackgroundResource(R.color.rowbgselector);

			imgIcon.setVisibility(View.VISIBLE);
			imgIcon.setImageResource(getImageId(pm.IconName));			
		}
		return v;
	}
	
	private Resources resources;
	private String packageName;
	
	private int getImageId(String name)
	{
		if (resources == null) resources = activity.getResources();
		if (packageName == null) packageName = activity.getPackageName();
		
		return resources.getIdentifier(name , "drawable", packageName);
	}
	
	private class ItemClickListener implements View.OnClickListener
	{
		private PrivateMessageItem pm;
		
		public ItemClickListener(PrivateMessageItem f)
		{
			this.pm = f;
		}
		
		@Override
		public void onClick(View v)
		{
			activity.onItemClicked(pm);
		}		
	}
}
